<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        border
      >
        <template #buy="{ row }">
          <span class="pointer blue" @click="openOffer(row)">查看</span>
        </template>
        <template #img="{ row }">
          <span
            v-if="row.pay_images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
        <template #actionBuy="{ row }">
          <el-button-group>
            <el-button-group>
              <!-- <el-button type="primary" size="small" @click="toDone(row)">
              已出单
            </el-button> -->
              <el-button type="danger" size="small" @click="cantBuy(row)">
                无法出单
              </el-button>
            </el-button-group>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 图片 -->
    <images v-model="showImages" :data="current" />
    <!-- 图片 -->
    <offer-list v-model="showOffer" :data="current" @done="done" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import { pageWatingBuyTrades, tradeBuyToChoose } from '@/api/company';
  import Images from './components/pay-images.vue';
  import Search from '@/views/insurance/components/search.vue';
  import OfferList from './components/offer-list.vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: '编号'
      },
      {
        width: 80,
        prop: 'area',
        label: '区域'
      },
      {
        width: 120,
        prop: 'plate_number',
        label: '车牌'
      },
      {
        prop: 'company',
        label: '车队'
      },
      {
        width: 100,
        slot: 'buy',
        label: '出单要求',
        align: 'center'
      },
      {
        width: 120,
        slot: 'img',
        label: '司机付款图片',
        align: 'center'
      },
      {
        width: 100,
        prop: 'create_user',
        label: '制单人'
      },
      {
        width: 100,
        prop: 'judge_user',
        label: '审核人'
      },
      {
        width: 140,
        prop: 'judge_time',
        label: '审核时间'
      }
    );

    return cols;
  });

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageWatingBuyTrades({
      ...where,
      ...orders,
      page,
      limit,
      status: 10
    });
    return result;
  };
  /** 搜索 */
  const reload = (where) => {
    tableRef.value?.reload?.({ page: 1, where });
  };

  const current = ref([]);

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  /** 查看出单要求 */
  const showOffer = ref(false);
  const openOffer = (row) => {
    current.value = row;
    showOffer.value = true;
  };

  /** 提交出单通知 */
  const cantBuy = (row = null) => {
    ElMessageBox.confirm(
      '确定退回“' + row.plate_number + '”的出单通知吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        tradeBuyToChoose({ id: row.id })
          .then(() => {
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            console.log(e);
          });
      })
      .catch(() => {});
  };

  const done = () => {
    showOffer.value = false;
    reload();
  };
</script>
